<?php

namespace app\common\service;
use app\common\service\AbstractService;
use app\common\service\CommonSystemImportInfoDetailService;
use app\order\logic\QcCheckLogic;
use app\order\logic\OrderCheckLogisticsLogic;
use app\order\logic\OrderShanHouLogic;
use app\order\logic\OrderDiFourLogic;
use app\customer\logic\OrderRefundLogic;
use app\customer\logic\ReviewsLogic;
use app\systems\model\CommonSystemImportDetailModel;
use app\systems\model\CommonSystemImportModel;
use app\api\logic\TestLogic;
use app\api\logic\LinXingOrderLogic;
use app\product\logic\ProductInventoryLogic;
use app\product\logic\ProductOnwayLogic;
use app\delivery\logic\DeliveryLogic;

class CommonSystemImportDetailService extends CommonSystemImportInfoDetailService {

    public function __construct() {
        $this->system_import_model        = new CommonSystemImportModel();
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
    }




    /**
     * incomplete_sku匹配捆绑导入
     * @param int $id
     * @throws \Exception
     * @author:luckyXiong
     * @Date: 2024/06/10
     */
    public function import_incomplete_time($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new TestLogic())->import_time_detail($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }




    /**
     * 产品库存数据导入
     * @author:luckyXiong
     * @Date: 2024-07-24
     */
    public function import_inventory_time($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new ProductInventoryLogic())->import_inventory_detail($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }


    /**
     * 产品在途库存数据导入
     * @author:luckyXiong
     * @Date: 2024-07-24
     */
    public function import_inventory_onway($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new ProductOnwayLogic())->import_inventory_onway($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }



    /**
     * 全程无忧账单数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2024/8/24
     */
    public function import_qc_page($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new QcCheckLogic())->import_qc_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }



    /**
     * 顺丰账单数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2024/8/24
     */
    public function import_sf_page($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new OrderCheckLogisticsLogic())->import_sf_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }



    /**
     * 闪电猴账单数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2024/9/2
     */
    public function import_hou_page($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new OrderShanHouLogic())->import_hou_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }





    /**
     * @title  递四方账单数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2024/9/2
     */
    public function import_fang_page($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new OrderDiFourLogic())->import_fang_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }



    /**
     * @title  退款数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2025/3/5
     */
    public function import_order_refund($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new OrderRefundLogic())->import_order_refund($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }




    /**
     * @title  备货建议数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2025/3/15
     */
    public function import_order_delivery($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new DeliveryLogic())->import_order_delivery($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }



    /**
     * @title  领星数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2025/3/15
     */
    public function import_order_lin_sku($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new LinXingOrderLogic())->import_order_lin_sku($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
             throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }


    /**
     * @title  退款数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2025/3/5
     */
    public function import_order_review_page($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new ReviewsLogic())->import_order_review_page($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }


}